package com.example.maybeaccount.Entry;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.util.Log;

import com.example.maybeaccount.MainActivity;
import com.example.maybeaccount.Mode.ActivityController;

/**
 * 生物识别
 */

public class BiometricPromptHelper  {
    private static final String TAG = "gryphon";
    private BiometricPrompt mBiometricPrompt;
    private CancellationSignal mCancellationSignal;
    private BiometricPrompt.AuthenticationCallback mAuthenticationCallback;
    private int back ;
    public void init(Activity mactivity){
        final Activity activity = mactivity;
        mBiometricPrompt = new BiometricPrompt.Builder(mactivity)
                .setTitle("生物认证")
                .setDescription("请选择认证方式")
                .setNegativeButton("取消", mactivity.getMainExecutor(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.i(TAG, "Cancel button clicked");
                    }
                })
                .build();

        mCancellationSignal = new CancellationSignal();
        mCancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
                //handle cancel result
                Log.i(TAG, "Canceled");
            }
        });

        mAuthenticationCallback = new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Log.i(TAG, "onAuthenticationError " + errString);
                back = -1;
            }

            @Override
            public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);

                Log.i(TAG, "onAuthenticationSucceeded " + result.toString());
                //Intent intent = new Intent(activity.getApplication(), MainActivity.class);
                //activity.startActivity(intent);
                ActivityController.resumeActivity(activity, activity);
                back  = 1;
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();

                Log.i(TAG, "onAuthenticationFailed ");
                back = 0;
            }
        };
        mBiometricPrompt.authenticate(mCancellationSignal, mactivity.getMainExecutor(), mAuthenticationCallback);

    }

    public int Getback(){
        return back;
    }


}
